import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;

public class PlanWriter {
    public void write_plan(){
        String file = "D:/java/xml/src/plan.xml";
        SAXReader reader = new SAXReader();
        try{
            Document document = reader.read(file);
            Element root = document.getRootElement();
            Element course = root.addElement("course");
            course.addAttribute("no","4");
            course.addElement("course_name").setText("java工程师");
            course.addElement("class_hour").setText("120");
            course.addElement("exam_form").setText("上机考试");
            Writer writer = new OutputStreamWriter(new FileOutputStream(file),"utf-8");
            document.write(writer);
            writer.close();
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    public static void main(String[] args) {
        PlanWriter planWriter = new PlanWriter();
        planWriter.write_plan();
    }
}
